# Created by yang
from django.conf.urls import url
from django.forms import ModelForm

from django.shortcuts import HttpResponse
from django.utils.safestring import mark_safe
from django.urls import reverse
from niubi.service import v1
from . import models
from django.forms import widgets as form_widgets


class UserInfoModelForm(ModelForm):
    class Meta:
        model = models.UserInfo
        fields = "__all__"
        widgets = {
            "name": form_widgets.TextInput(attrs={'class': 'form-control'}),
            "email": form_widgets.TextInput(attrs={'class': 'form-control'})
        }


class PermissionConfig(object):
    show_add_btn = True

    def get_show_add_btn(self):
        print(self.request)
        return self.show_add_btn

    def get_list_display(self):
        return super().get_list_display()

    def get_actions(self):
        return super().get_actions()


class UserInfoConfig(PermissionConfig, v1.ModelNb):
    # model_form = UserInfoModelForm

    def test(self, request):
        return HttpResponse("test")

    def extra_urls(self):
        partterns = [
            url(r'test/$', self.test)
        ]
        return partterns

    def xxxx(self, obj=None, isheader=False):
        if isheader:
            return '列名称'
        else:
            return obj.name + obj.email

            # 增删改查插件--filter--传递函数

    def func(self, chang_list_obj, option):

        data_list = models.Role.objects.filter(id__gt=2)
        return v1.RowItems(option, chang_list_obj, data_list, chang_list_obj.model_config.request)

    list_display = ["id", "name", "email", xxxx]
    list_filter = [
        v1.FilterOption("group", True, lambda x:x.title, lambda x:x.id),
        v1.FilterOption("roles", False, lambda x:x.name, lambda x:x.id),
        v1.FilterOption(func, True, lambda x: x.name, lambda x: x.id),
    ]


class RoleInfoConfig(v1.ModelNb):
    list_display = ["id", "name"]


v1.site.register(models.UserInfo, UserInfoConfig)
v1.site.register(models.UserGroup)
v1.site.register(models.Role, RoleInfoConfig)
